﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.master" AutoEventWireup="true" CodeBehind="SalePerDay.aspx.cs" Inherits="Project_Udrinkwater.Empoyee_Management.SalePerDay" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:Panel ID="Panel2" runat="server">
        <fieldset>
        <legend>ตารางแสดงการขายประจำวัน:</legend>
        <dx:ASPxButton ID="ASPxButton2" runat="server" onclick="ASPxButton2_Click" 
            Text="สร้างรายการขาย" Width="120px">
        </dx:ASPxButton>
        <dx:ASPxGridView ID="MainGrid" runat="server" 
    Width="100%" ClientInstanceName="Mgrid" AutoGenerateColumns="False" 
            DataSourceID="SqlOrder" KeyFieldName="Order_ID">
            <Columns>
                <dx:GridViewCommandColumn VisibleIndex="8" Width="100px">
                    <EditButton Visible="True" Text="แก้ไข">
                    </EditButton>
                    <DeleteButton Visible="True" Text="ลบ">
                    </DeleteButton>
                    <CancelButton Text="ยกเลิก">
                    </CancelButton>
                    <UpdateButton Text="ตกลง">
                    </UpdateButton>
                    <ClearFilterButton Visible="True" Text="ยกเลิก">
                    </ClearFilterButton>
                </dx:GridViewCommandColumn>
                <dx:GridViewDataTextColumn FieldName="Order_ID" ReadOnly="True"
                    VisibleIndex="0" Visible="False">
                    <EditFormSettings Visible="False" />
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataTextColumn FieldName="Order_doc_NO" ReadOnly="True"
                    VisibleIndex="1" Width="150px" Caption="เลขที่เอกสาร">
                    <EditFormSettings Visible="False" />
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataDateColumn FieldName="Order_Date_Time" VisibleIndex="2" 
                    Width="100px" Caption="วันที่เอกสาร">
                    <PropertiesDateEdit DisplayFormatString="dd/MM/yyyy" EditFormat="Custom" 
                        EditFormatString="dd/MM/yyyy">
                    </PropertiesDateEdit>
                </dx:GridViewDataDateColumn>
                <dx:GridViewDataTextColumn FieldName="Credit" VisibleIndex="3" Visible="False">
                </dx:GridViewDataTextColumn>
                <dx:GridViewDataDateColumn FieldName="Deadlines_Date" VisibleIndex="4" 
                    Width="100px" Caption="วันที่ส่ง">
                    <PropertiesDateEdit DisplayFormatString="dd/MM/yyyy" EditFormat="Custom" 
                        EditFormatString="dd/MM/yyyy">
                    </PropertiesDateEdit>
                </dx:GridViewDataDateColumn>
                <dx:GridViewDataComboBoxColumn FieldName="Employee_ID" VisibleIndex="5" 
                    Caption="พนักงานขาย" Width="100px">
                    <PropertiesComboBox DataSourceID="SqlEmp" TextField="Name" 
                        ValueField="Employee_ID">
                        <Columns>
                            <dx:ListBoxColumn FieldName="Employee_ID" />
                            <dx:ListBoxColumn FieldName="Name" />
                            <dx:ListBoxColumn FieldName="Department_Name" />
                        </Columns>
                    </PropertiesComboBox>
                </dx:GridViewDataComboBoxColumn>
                <dx:GridViewDataComboBoxColumn FieldName="Customer_ID" VisibleIndex="6" 
                    Caption="ลูกค้า" Width="100px">
                    <PropertiesComboBox DataSourceID="SqlCustomer" TextField="Name" 
                        ValueField="Customer_ID">
                        <Columns>
                            <dx:ListBoxColumn FieldName="Customer_ID" />
                            <dx:ListBoxColumn FieldName="Name" />
                        </Columns>
                    </PropertiesComboBox>
                </dx:GridViewDataComboBoxColumn>
                <dx:GridViewDataTextColumn VisibleIndex="7" Width="70px" Caption="รายละเอียด">
                    <EditFormSettings Visible="False" />
                    <DataItemTemplate>
                        <asp:LinkButton ID="LinkButton2" runat="server"  CommandArgument='<%# Eval("Order_ID") %>'  oncommand="LinkButton2_Command">ดูรายการ</asp:LinkButton>
                    </DataItemTemplate>
                </dx:GridViewDataTextColumn>
            </Columns>
            <Settings ShowFilterRow="True" />
        </dx:ASPxGridView>
            <asp:SqlDataSource ID="SqlCustomer" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                SelectCommand="SELECT  Firstname  +'  '+ Lastname AS Name ,Customer_ID FROM TB_Customer">
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlOrder" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                DeleteCommand="DELETE FROM TB_Order WHERE (Order_ID = @Order_ID)" 
                InsertCommand="INSERT INTO TB_Order(Order_Date_Time, Credit, Deadlines_Date, Employee_ID, Customer_ID) VALUES (@Order_Date_Time, @Credit, @Deadlines_Date, @Employee_ID, @Customer_ID)" 
                SelectCommand="SELECT Order_ID, Order_doc_NO, Order_Date_Time, Credit, Deadlines_Date, Employee_ID, Customer_ID FROM TB_Order WHERE Order_Date_Time = CAST(GETDATE() AS DATE) ORDER BY Order_ID DESC" 
                UpdateCommand="UPDATE TB_Order SET Order_Date_Time = @Order_Date_Time  , Credit = @Credit , Deadlines_Date = @Deadlines_Date  , Employee_ID = @Employee_ID , Customer_ID = @Customer_ID   WHERE (Order_ID = @Order_ID)">
                <DeleteParameters>
                    <asp:Parameter Name="Order_ID" />
                </DeleteParameters>
                <InsertParameters>
                    <asp:Parameter Name="Order_Date_Time" />
                    <asp:Parameter Name="Credit" />
                    <asp:Parameter Name="Deadlines_Date" />
                    <asp:Parameter Name="Employee_ID" />
                    <asp:Parameter Name="Customer_ID" />
                </InsertParameters>
                <UpdateParameters>
                    <asp:Parameter Name="Order_Date_Time" />
                    <asp:Parameter Name="Credit" />
                    <asp:Parameter Name="Deadlines_Date" />
                    <asp:Parameter Name="Employee_ID" />
                    <asp:Parameter Name="Customer_ID" />
                    <asp:Parameter Name="Order_ID" />
                </UpdateParameters>
            </asp:SqlDataSource>
            <asp:SqlDataSource ID="SqlEmp" runat="server" 
                ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                SelectCommand="SELECT TB_Employee.Employee_ID, TB_Employee.FirstName + ' ' + TB_Employee.LastName AS Name, TB_Department.Department_Name FROM TB_Employee INNER JOIN TB_Department ON TB_Employee.Departmernt_ID = TB_Department.Department_ID">
            </asp:SqlDataSource>
         </fieldset>
    </asp:Panel>
    <asp:Panel ID="Panel3" runat="server" Visible="False">
         <fieldset>
         <legend>รายการขาย:</legend>
        <table style="width: 100%">
            <tr>
                <td style="width: 140px">
                    <dx:ASPxButton ID="ASPxButton1" runat="server" onclick="ASPxButton1_Click" 
                        Text="กลับรายการหลัก" Width="118px">
                    </dx:ASPxButton>
                </td>
                <td style="width: 144px">
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
                <td style="width: 202px">
                    &nbsp;</td>
                <td style="width: 401px">
                    &nbsp;</td>
                <td align="right">
                    <dx:ASPxButton ID="ASPxButton3" runat="server" onclick="ASPxButton3_Click" 
                        Text="เพิ่มรายการ">
                    </dx:ASPxButton>
                </td>
            </tr>
        </table>
             <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                 <ContentTemplate>
                     <dx:ASPxGridView ID="DetailGrid" runat="server" AutoGenerateColumns="False" 
                         ClientInstanceName="Dgrid" DataSourceID="SqlOderList" 
                         KeyFieldName="id" 
                         onrowinserting="DetailGrid_RowInserting" 
                         onrowupdating="DetailGrid_RowUpdating" Width="100%" 
                         onhtmleditformcreated="DetailGrid_HtmlEditFormCreated">
                         <TotalSummary>
                             <dx:ASPxSummaryItem DisplayFormat="รวม {0:#,###.00}" FieldName="Total" 
                                 ShowInColumn="Total" ShowInGroupFooterColumn="Total" SummaryType="Sum" />
                         </TotalSummary>
                         <Columns>
                             <dx:GridViewCommandColumn VisibleIndex="10">
                                 <EditButton Visible="True" Text="แก้ไข">
                                 </EditButton>
                                 <NewButton Text="เพิ่ม" Visible="True">
                                 </NewButton>
                                 <DeleteButton Visible="True" Text="ลบ">
                                 </DeleteButton>
                                 <CancelButton Text="ยกเลิก">
                                 </CancelButton>
                                 <UpdateButton Text="ตกลง">
                                 </UpdateButton>
                                 <ClearFilterButton Visible="True" Text="ยกเลิก">
                                 </ClearFilterButton>
                             </dx:GridViewCommandColumn>
                             <dx:GridViewDataTextColumn FieldName="Order_ID" ReadOnly="True" Visible="False" 
                                 VisibleIndex="0">
                                 <EditFormSettings Visible="False" />
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataTextColumn FieldName="Order_doc_NO" ReadOnly="True" 
                                 VisibleIndex="2" Width="150px" Caption="เลขที่เอกสาร">
                                 <EditFormSettings Visible="False" />
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataTextColumn FieldName="Row" ReadOnly="True" VisibleIndex="1" 
                                 Caption="ลำดับ">
                                 <EditFormSettings Visible="False" />
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataTextColumn FieldName="id" ReadOnly="True" Visible="False" 
                                 VisibleIndex="3">
                                 <EditFormSettings Visible="False" />
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataComboBoxColumn FieldName="Order_Category_ID" VisibleIndex="5" 
                                 Width="150px" Caption="ประเภทการขาย">
                                 <PropertiesComboBox DataSourceID="SqlOrdercateAll" 
                                     TextField="Order_Category_Name" ValueField="Order_Category_ID">
                                 </PropertiesComboBox>
                             </dx:GridViewDataComboBoxColumn>
                             <dx:GridViewDataComboBoxColumn FieldName="Product_ID" VisibleIndex="4" 
                                 Width="150px" Caption="สินค้า">
                                 <PropertiesComboBox DataSourceID="SqlProductAll" TextField="Product_Name" 
                                     ValueField="Product_ID">
                                 </PropertiesComboBox>
                             </dx:GridViewDataComboBoxColumn>
                             <dx:GridViewDataTextColumn FieldName="Price_Unit" VisibleIndex="6" 
                                 Caption="ราคา">
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataTextColumn FieldName="Amount" VisibleIndex="7" Caption="จำนวน">
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataTextColumn FieldName="Discount" VisibleIndex="8" 
                                 Caption="ส่วนลด">
                             </dx:GridViewDataTextColumn>
                             <dx:GridViewDataTextColumn FieldName="Total" ReadOnly="True" VisibleIndex="9" 
                                 Caption="รวม">
                                 <EditFormSettings Visible="False" />
                             </dx:GridViewDataTextColumn>
                         </Columns>
                         <SettingsPager Mode="ShowAllRecords">
                         </SettingsPager>
                         <SettingsEditing Mode="EditForm" />
                         <Settings ShowFilterRow="True" ShowFooter="True" />
                         <SettingsPopup>
                             <EditForm Modal="True" Width="600px" />
                         </SettingsPopup>
                         <Templates>
                             <EditForm>
                                 <table style="width: 100%">
                                     <tr>
                                         <td align="right" style="width: 243px; height: 27px;">
                                             <dx:ASPxLabel ID="ASPxLabel1" runat="server" Text="สินค้า:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td style="height: 27px">
                                             <dx:ASPxComboBox ID="cmbProduct" runat="server" AutoPostBack="True" 
                                                 DataSourceID="SqlProductAll" 
                                                 onselectedindexchanged="cmbProduct_SelectedIndexChanged" 
                                                 TextField="Product_Name" ValueField="Product_ID" Width="200px">
                                             </dx:ASPxComboBox>
                                         </td>
                                         <td style="width: 181px; height: 27px;">
                                             <dx:ASPxLabel ID="ASPxLabel2" runat="server" Text="ประเภทการขาย:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td style="width: 214px; height: 27px;">
                                             <dx:ASPxComboBox ID="cmbOrdercate" runat="server" AutoPostBack="True" 
                                                 DataSourceID="SqlOrdercate" 
                                                 onselectedindexchanged="cmbOrdercate_SelectedIndexChanged" 
                                                 TextField="Order_Category_Name" ValueField="Order_Category_ID" 
                                                 Width="200px" ValueType="System.Int32">
                                             </dx:ASPxComboBox>
                                         </td>
                                         <td style="width: 181px; height: 27px;">
                                             &nbsp;</td>
                                     </tr>
                                     <tr>
                                         <td align="right" style="width: 243px">
                                             <dx:ASPxLabel ID="ASPxLabel3" runat="server" Text="ราคา">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td>
                                             <dx:ASPxComboBox ID="cmbPrice" runat="server" DataSourceID="SqlPrice" 
                                                 TextField="Price" ValueField="Price" ValueType="System.Int32" Width="200px">
                                             </dx:ASPxComboBox>
                                         </td>
                                         <td style="width: 181px">
                                             <dx:ASPxLabel ID="ASPxLabel4" runat="server" Text="จำนวน:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td style="width: 181px" colspan="2">
                                             <dx:ASPxTextBox ID="txtAmount" runat="server" Width="200px">
                                             </dx:ASPxTextBox>
                                         </td>
                                     </tr>
                                     <tr>
                                         <td align="right" style="width: 243px">
                                             <dx:ASPxLabel ID="ASPxLabel5" runat="server" Text="ส่วนลด:">
                                             </dx:ASPxLabel>
                                         </td>
                                         <td>
                                             <dx:ASPxTextBox ID="txtDiscount" runat="server" Width="200px">
                                             </dx:ASPxTextBox>
                                         </td>
                                         <td style="width: 181px">
                                             &nbsp;</td>
                                         <td style="width: 214px">
                                             &nbsp;</td>
                                         <td style="width: 181px">
                                             &nbsp;</td>
                                     </tr>
                                 </table>
                                 <div style="text-align: right; padding: 2px 2px 2px 2px">
                    <dx:ASPxGridViewTemplateReplacement ID="UpdateButton" ReplacementType="EditFormUpdateButton"
                        runat="server">
                    </dx:ASPxGridViewTemplateReplacement>
                    <dx:ASPxGridViewTemplateReplacement ID="CancelButton" ReplacementType="EditFormCancelButton"
                        runat="server">
                    </dx:ASPxGridViewTemplateReplacement>
                </div>
                             </EditForm>

                         </Templates>
                     </dx:ASPxGridView>
                 </ContentTemplate>
             </asp:UpdatePanel>
             <asp:SqlDataSource ID="SqlOderList" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 DeleteCommand="DELETE FROM TB_Order_List WHERE (id = @id)" 
                 InsertCommand="INSERT INTO TB_Order_List(Order_ID, Order_Category_ID, Product_ID, Amount, Price_Unit, Discount) VALUES (@Order_ID, @Order_Category_ID, @Product_ID, @Amount, @Price_Unit, @Discount);" 
                 SelectCommand="SELECT TB_Order.Order_ID, TB_Order.Order_doc_NO,ROW_NUMBER() OVER(ORDER BY TB_Order.Order_ID DESC) AS Row, TB_Order_List.id, TB_Order_List.Order_Category_ID, TB_Order_List.Product_ID, TB_Order_List.Price_Unit, TB_Order_List.Amount, TB_Order_List.Discount , (TB_Order_List.Price_Unit *TB_Order_List.Amount-TB_Order_List.Discount) AS Total FROM TB_Order INNER JOIN TB_Order_List ON TB_Order.Order_ID = TB_Order_List.Order_ID INNER JOIN TB_Order_Category ON TB_Order_List.Order_Category_ID = TB_Order_Category.Order_Category_ID  WHERE (TB_Order.Order_ID = @Order_ID)" 
                 UpdateCommand="UPDATE TB_Order_List SET Order_Category_ID = @Order_Category_ID, Product_ID = @Product_ID, Amount = @Amount, Price_Unit = @Price_Unit , Discount = @Discount  WHERE (id = @id)">
                 <DeleteParameters>
                     <asp:Parameter Name="id" />
                 </DeleteParameters>
                 <InsertParameters>
                     <asp:SessionParameter Name="Order_ID" SessionField="MOrder" />
                     <asp:Parameter Name="Order_Category_ID" />
                     <asp:Parameter Name="Product_ID" />
                     <asp:Parameter Name="Amount" />
                     <asp:Parameter Name="Price_Unit" />
                     <asp:Parameter Name="Discount" />
                 </InsertParameters>
                 <SelectParameters>
                     <asp:SessionParameter Name="Order_ID" SessionField="MOrder" />
                 </SelectParameters>
                 <UpdateParameters>
                     <asp:Parameter Name="Order_Category_ID" />
                     <asp:Parameter Name="Product_ID" />
                     <asp:Parameter Name="Amount" />
                     <asp:Parameter Name="Price_Unit" />
                     <asp:Parameter Name="Discount" />
                     <asp:Parameter Name="id" />
                 </UpdateParameters>
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlOrdercate" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 SelectCommand="SELECT Order_Category_ID, Order_Category_Name, Price FROM TB_Order_Category WHERE (Product_ID = @Product_ID)">
                 <SelectParameters>
                     <asp:SessionParameter  Name="Product_ID" 
                         SessionField="sProduct_ID" />
                 </SelectParameters>
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlOrdercateAll" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 SelectCommand="SELECT [Order_Category_ID], [Order_Category_Name] FROM [TB_Order_Category]">
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlPrice" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" SelectCommand="SELECT        Order_Category_ID, Order_Category_Name, Price
FROM            TB_Order_Category
WHERE        (Order_Category_ID= @Order_Category_ID)">
                 <SelectParameters>
                     <asp:SessionParameter Name="Order_Category_ID" SessionField="sOrderCatID" />
                 </SelectParameters>
             </asp:SqlDataSource>
             <asp:SqlDataSource ID="SqlProductAll" runat="server" 
                 ConnectionString="<%$ ConnectionStrings:DB_UdrinkConnectionString %>" 
                 SelectCommand="SELECT * FROM [TB_Product]"></asp:SqlDataSource>
        </fieldset>
    </asp:Panel>

</asp:Content>
